#!/usr/bin/env python

# Copyright 2012 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""OAuth 2 Client Credentials."""

import base64
import urllib
from oauth2client.client import OAuth2Credentials

__author__ = "Maciej Machulak"
__maintainer__ = "Maciej Machulak"
__email__ = "mmachulak@google.com"

__copyright__ = "Copyright 2012 Google Inc. All Rights Reserved."
__license__ = "Apache License 2.0"
__version__ = "0.2"
__status__ = "Prototype"


class ClientCredentials(OAuth2Credentials):

    client_id = None
    client_secret = None

    def __init__(self, client_id, client_secret, token_uri, user_agent=None):
        """Constructor for ClientCredentials.

        Args:
          client_id: client_id of the application
          client_secret: client_secret of the application
          token_uri: string, URI for token endpoint.
          user_agent: string, The HTTP User-Agent to provide for this
          application.
        """
        super(ClientCredentials, self).__init__(
            None,
            None,
            None,
            None,
            None,
            token_uri,
            user_agent)
        self.client_id = client_id
        self.client_secret = client_secret

    def _generate_refresh_request_body(self):
        body = urllib.urlencode({
            'client_id': self.client_id,
            'client_secret': self.client_secret,
            'grant_type': 'client_credentials',
            })

        return body


class HeaderClientCredentials(ClientCredentials):

    def __init__(self, client_id, client_secret, token_uri, user_agent=None):
        """Constructor for ClientCredentials.

        Args:
          client_id: client_id of the application
          client_secret: client_secret of the application
          token_uri: string, URI for token endpoint.
          user_agent: string, The HTTP User-Agent to provide for this
          application.
        """
        super(HeaderClientCredentials, self).__init__(
            client_id, client_secret, token_uri, user_agent)

    def _generate_refresh_request_body(self):
        body = urllib.urlencode({
            'grant_type': 'client_credentials',
            })

        return body

    def _generate_refresh_request_headers(self):
        """Generate the headers that will be used in the refresh request."""
        # See RFC2617
        credentials = self.client_id + ':' + self.client_secret
        headers = {
            'content-type': 'application/x-www-form-urlencoded',
            'authorization': 'basic ' + base64.b64encode(credentials)
        }

        if self.user_agent is not None:
            headers['user-agent'] = self.user_agent

        return headers
